
//分页
var pageEvent=function(obj){
    console.log(obj);
    if(obj.total_page){
        layui.use('laypage', function(){
            var laypage = layui.laypage;
            var auth_token=obj.auth_token;
            //执行一个laypage实例
            laypage.render({
                elem: 'test3' //注意，这里的 test1 是 ID，不用加 # 号
                ,prev:'<i class="pageIcon left"></i>'
                ,next:'<i class="pageIcon right"></i>'
                ,count: obj.total_page*18 //数据总数，从服务端得到
                ,curr:obj.page_no
                ,layout: ['prev', 'page', 'next']
                ,limit:obj.page_size
                ,theme:"list"
                ,jump: function(obj, first){
                    console.log(obj);
                    //首次不执行
                    if(!first){
                        topicFilter(auth_token,obj.curr)
                    }
                }
            });
        });
    }

};

//继续答题
var groupContinue=function(){
    $(".continueBut").click(function(){
        var statusArr={
            'auth_token':tokens,
            'group_id':$(this).attr('data-group_id'),
            'group_type':$(this).attr('data-group_type'),
            'mode':$(this).attr('data-mode'),
            'history_id':$(this).attr('data-history_id')
        };
        console.log(statusArr);
        $.cookie('statusArr',JSON.stringify(statusArr));
        $.cookie('statusArrBool',true);
        setTimeout(function(){
            if(parseInt(statusArr.mode)!==1){
                setUrl('screen','overyears/list')//设置跳转
            }else{
                setUrl('screen','overyears/listReview')//设置跳转
            }

        },500);
    });
};

//请求课程分类列表数据
var topicList=function(token,num){
    var arr1={
        auth_token:token,
        page_no:num||1,
        page_size:18
    };
    $.ajax({
        type: "GET",
        url:base_url + "practice/list",
        data:arr1,
        beforeSend:function(XMLHttpRequest){},
        success:function(data){
            console.log(data);
            var datas=data.data;
            if(datas){
                var total_page=datas.total_page;
                var page_size=datas.page_size;
                var page_no=datas.page_no;
                var page_contents=datas.page_contents;
                $(".screenTable table tr").each(function(){
                    if($(this).index()>0){
                        $(this).remove()
                    }
                });
                for(var i=0;i<page_contents.length;i++){
                    var group_desc=page_contents[i].group_desc;
                    var group_id=page_contents[i].group_id;
                    var history_id=page_contents[i].history_id;
                    var practice_duration=page_contents[i].practice_duration;
                    var last_time=page_contents[i].last_time;
                    var group_type=page_contents[i].group_type;
                    var group_list=['真题和模拟题','真题和模拟题','问题类型练习题'];
                    var submit_status=page_contents[i].submit_status;
                    var submit_txt='';
                    var button='';

                    var groupArr={
                        'auth_token':token,
                        'group_id':group_id,
                        'group_type':group_type,
                        'mode':2,
                        'history_id':history_id
                    };
                    if(submit_status==1){
                        submit_txt='已完成';
                        button='<button data-group_id="'+groupArr.group_id+'" data-group_type="'+groupArr.group_type+'" data-mode="1" data-history_id="'+groupArr.history_id+'" class="continueBut on">复习题目</button><button data-group_id="'+groupArr.group_id+'" data-group_type="'+groupArr.group_type+'" data-mode="'+groupArr.mode+'" data-history_id="0" class="continueBut">重新做题</button>'
                    }else if(submit_status==0){
                        submit_txt='进行中';
                        button='<button data-group_id="'+groupArr.group_id+'" data-group_type="'+groupArr.group_type+'" data-mode="'+groupArr.mode+'" data-history_id="'+groupArr.history_id+'" class="continueBut on">继续做题</button><button data-group_id="'+groupArr.group_id+'" data-group_type="'+groupArr.group_type+'" data-mode="'+groupArr.mode+'" data-history_id="0" class="continueBut">重新做题</button>'
                    }if(submit_status==2){
                        submit_txt='已放弃';
                        button='<button data-group_id="'+groupArr.group_id+'" data-group_type="'+groupArr.group_type+'" data-mode="'+groupArr.mode+'" data-history_id="0" class="continueBut">重新做题</button>'
                    }

                    var li='<tr>\n' +
                        '                                <td>'+group_desc+'</td>\n' +
                        '                                <td>'+group_list[group_type-1]+'</td>\n' +
                        '                                <td>'+submit_txt+'</td>\n' +
                        '                                <td>'+last_time+'</td>\n' +
                        '                                <td>'+formatSeconds(practice_duration)+'</td>\n' +
                        '                                <td>'+button+'</td>\n' +
                        '                            </tr>';

                    $(".screenTable table").append(li);
                }
                groupContinue();

                //分页注册事件
                var listPage={
                    'page_no':page_no,
                    'page_size':page_size,
                    'total_page':total_page,
                    'auth_token':token
                };
                pageEvent(listPage)

            }
        },
        error:function(){
            console.log('失败')
        }

    });
};

var topicFilter=function(token,num){
    var practice_paper_type=$(".screenSelectInput").eq(0).attr("data-val");
    var status=$(".screenSelectInput").eq(1).attr("data-val");
    var days=$(".screenSelectInput").eq(2).attr("data-val");
    console.log(practice_paper_type);

    var arr1={
        'auth_token':token,
        'page_no':num||1,
        'page_size':18,
        'practice_paper_type':practice_paper_type,
        'status':status,
        'days':days
    };
    $.ajax({
        type: "GET",
        url:base_url + "practice/filter",
        data:arr1,
        beforeSend:function(XMLHttpRequest){},
        success:function(data){
            console.log(data);
            var datas=data.data;
            if(datas){
                var total_page=datas.total_page;
                var page_size=datas.page_size;
                var page_no=datas.page_no;
                var page_contents=datas.page_contents;
                $(".screenTable table tr").each(function(){
                    if($(this).index()>0){
                        $(this).remove()
                    }
                });
                for(var i=0;i<page_contents.length;i++){
                    var group_desc=page_contents[i].group_desc;
                    var group_id=page_contents[i].group_id;
                    var history_id=page_contents[i].history_id;
                    var practice_duration=page_contents[i].practice_duration;
                    var last_time=page_contents[i].last_time;
                    var group_type=page_contents[i].group_type;
                    var tp_type=page_contents[i].tp_type;
                    var group_list=['真题','模拟题','知识点练习题','问题类型练习题'];
                    var submit_status=page_contents[i].submit_status;
                    var submit_txt='';
                    var button='';

                    var groupArr={
                        'auth_token':token,
                        'group_id':group_id,
                        'group_type':group_type,
                        'mode':2,
                        'history_id':history_id
                    };
                    if(submit_status==1){
                        submit_txt='已完成';
                        button='<button data-group_id="'+groupArr.group_id+'" data-group_type="'+groupArr.group_type+'" data-mode="1" data-history_id="'+groupArr.history_id+'" class="continueBut on">复习题目</button><button data-group_id="'+groupArr.group_id+'" data-group_type="'+groupArr.group_type+'" data-mode="'+groupArr.mode+'" data-history_id="0" class="continueBut">重新做题</button>'
                    }else if(submit_status==0){
                        submit_txt='进行中';
                        button='<button data-group_id="'+groupArr.group_id+'" data-group_type="'+groupArr.group_type+'" data-mode="'+groupArr.mode+'" data-history_id="'+groupArr.history_id+'" class="continueBut on">继续做题</button><button data-group_id="'+groupArr.group_id+'" data-group_type="'+groupArr.group_type+'" data-mode="'+groupArr.mode+'" data-history_id="0" class="continueBut">重新做题</button>'
                    }if(submit_status==2){
                        submit_txt='已放弃';
                        button='<button data-group_id="'+groupArr.group_id+'" data-group_type="'+groupArr.group_type+'" data-mode="'+groupArr.mode+'" data-history_id="0" class="continueBut">重新做题</button>'
                    }

                    var li='<tr>\n' +
                        '                                <td>'+group_desc+'</td>\n' +
                        '                                <td>'+group_list[tp_type]+'</td>\n' +
                        '                                <td>'+submit_txt+'</td>\n' +
                        '                                <td>'+last_time+'</td>\n' +
                        '                                <td>'+formatSeconds(practice_duration)+'</td>\n' +
                        '                                <td>'+button+'</td>\n' +
                        '                            </tr>';

                    $(".screenTable table").append(li);
                }
                groupContinue();

                //分页注册事件
                var listPage={
                    'page_no':page_no,
                    'page_size':page_size,
                    'total_page':total_page,
                    'auth_token':token
                };
                pageEvent(listPage)

            }
        },
        error:function(){
            console.log('失败')
        }

    });
};

$(function(){

    $(".screenSelectInputTog").each(function(){
        var selectTxt=$(this).find("select option:selected");
        console.log(selectTxt.val());
        $(this).siblings('.screenSelectInput').text(selectTxt.text()).attr('data-val',selectTxt.val());
    });
    //下拉框菜单
    $(".screenSelectInputTog select").change(function(){
        var valVal=$(this).find('option:selected').val();//选中的value
        var valTxt=$(this).find('option:selected').text();//选中的值
        console.log(valTxt);
        $(this).parents(".screenSelectInputTog").siblings('.screenSelectInput').text(valTxt).attr('data-val',valVal);
    });

    //判断token
    if(tokens){
        topicFilter(tokens);
        $(".screenSelectButton").unbind('click').click(function(){
            topicFilter(tokens);
        })
    }else{
        layTxt('您还没登录!');
        setTimeout(function(){
            setUrl('screen','login')//设置跳转
        },1000)
    }

});
//时间
layui.use('laydate', function() {
    var laydate = layui.laydate;
    //常规用法
    laydate.render({
        elem: '#test1'
    });
    laydate.render({
        elem: '#test2'
    });
});

//echarts
var echartData={
    "dataX":['0', '1', '2', '3', '4', '5', '6'],
    "dataY":[820, 932, 901, 934, 1290, 1330, 1320]
};
echartEvent(echartData);